using System.IO;
using System.Text;
using LDNS.Interfaces;
using LDNS.Models;
using LDNS.Services;
using LDNS.SDK.Commands.Models;
using LDNS.Utilities;

namespace LDNS.Commands
{
    /// <summary>
    /// 列出记录命令处理器
    /// 处理LIST_RECORDS命令，列出所有DNS解析记录
    /// 作者：huobing
    /// </summary>
    public class ListRecordsCommandHandler : CommandHandlerBase<ListRecordsCommand, DnsRecordsResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => ListRecordsCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志记录器</param>
        /// <param name="dnsService">DNS服务实例</param>
        public ListRecordsCommandHandler()
        {}
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            DnsRecordsResult dnsRecordsResult = CreateSuccessResult("查询成功");
            dnsRecordsResult.Records = new List<DnsRecordItem>();
            
            // 遍历DnsRecordManager中的所有DNS记录并转换为DnsRecordItem
            foreach (var dnsRecord in DnsRecordManager.Instance.DnsRecords.Values)
            {
                // 创建新的DnsRecordItem并映射属性
                var recordItem = new DnsRecordItem
                {
                    Domain = dnsRecord.Domain,       // 域名映射
                    IpAddress = dnsRecord.IP,        // IP地址映射
                    RecordType = dnsRecord.RecordType, // 记录类型映射
                    Ttl = dnsRecord.Ttl,             // TTL值映射
                    Description = dnsRecord.Description // 描述信息映射
                };
                
                // 添加到结果列表中
                dnsRecordsResult.Records.Add(recordItem);
            }
            
            return dnsRecordsResult;
        }
    }
}